package com.example.handler;

import com.example.domain.question.HotQuestion;
import com.example.domain.question.HotQuestionVO;
import com.example.mapper.question.QuestionHotMapper;
import com.example.mapper.question.QuestionMapper;
import com.example.redis.service.RedisService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import oj.common.core.contants.CacheConstants;
import oj.common.core.domain.PageQueryDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class QuestionXxlJob {
    @Autowired
    private QuestionHotMapper questionHotMapper;
    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private RedisService redisService;

    @XxlJob("questionHotListOrganizeHandler")
    public void questionHotListOrganizeHandler() {
        log.info("题目热榜进行刷新");
        PageQueryDTO pageQueryDTO = new PageQueryDTO();
        //固定三条大小的数量
        pageQueryDTO.setPageSize(CacheConstants.QUESTION_HOT_SIZE);
        List<HotQuestion> hotQuestionList = questionHotMapper.selectHotQuestion(pageQueryDTO);
        List<Long> questionIdList = hotQuestionList.stream().map(HotQuestion::getQuestionId).toList();
        //将题目热表的id存储到redis中，然后再从redis去查询
        List<HotQuestionVO> questionHotVOList = questionMapper.selectHotQuestionTitle(questionIdList);
        redisService.rightPushAll(CacheConstants.QUESTION_HOST_LIST,questionHotVOList);
    }
}
